<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Memcache 查看列出所有key方法 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Memcache 查看列出所有key方法</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Memcached/">Memcached</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;今天在做一个Memcache的session测试，但是在测试的过程中，发现Memcache没有一个比较简单的方法可以直接象redis那样keys *列出所有的Session key，并根据key get对应 的session内容</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;于是，我开始查找资料，翻出来的大部分是一些memcache常用命令等</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但是对列出key的办法，讲解却不多，于是来到google，找到了一个国外的资料</p>
<p><a href="http://www.darkcoding.net/software/memcached-list-all-keys/" target="_blank" rel="external">http://www.darkcoding.net/software/memcached-list-all-keys/</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cmd上登录memcache</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;telnet 127.0.0.1 11211</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具体的内容我套用我的测试环境中，操作如下</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先列出所有keys</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">stats items</div><div class="line">STAT items:7:number 1</div><div class="line">STAT items:7:age 188</div><div class="line">END</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来基于列出的items id，本例中为7，第2个参数为列出的长度，0为全部列出</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">stats cachedump 7 0</div><div class="line">ITEM Sess_sidsvpc1473t1np08qnkvhf6j2 [183 b; 1394527347 s]</div><div class="line">END</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的stats cachedump命令列出了我的session key，接下来就用get命令查找对应的 session值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">get Sess_sidsvpc1473t1np08qnkvhf6j2</div><div class="line">VALUE</div><div class="line">Sess_sidsvpc1473t1np08qnkvhf6j2 1440 1</div><div class="line">83</div><div class="line">Sess_|a:5:&#123;s:6:”verify”;s:32:”e70981fd305170c41a5632b2a24bbcaa”;s:3:”uid”;s:1:”1</div><div class="line">“;s:8:”username”;s:5:”admin”;s:9:”logintime”;s:19:”2014-03-11 16:24:25″;s:7:”<span class="built_in">log</span></div><div class="line">inip”;s:9:”127.0.0.1″;&#125;</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Memcached/3. Memcache 查看列出所有key方法/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
